package com.airsystem.pos.bengkel.controller;

import javax.accessibility.Accessible;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.entity.DetailLayanan;
import com.airsystem.pos.bengkel.interfaces.IMaster;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.interfaces.IValidator;
import com.airsystem.pos.bengkel.model.MasterServicesModel;
import com.airsystem.pos.bengkel.service.LayananServices;
import com.airsystem.pos.bengkel.util.FormValidator;
import com.airsystem.pos.bengkel.view.dialog.MasterServiceDialog;
import com.airsystem.pos.bengkel.view.dialog.MasterServiceTypeDialog;
import com.airsystem.pos.bengkel.view.dialog.MasterVehicleDialog;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class MasterServicesController extends MasterServicesModel implements IMaster {
	private static final Logger LOG = Logger.getLogger(MasterServicesController.class.getSimpleName());

	private IValidator<Accessible> formValidator;

	private IServices<DetailLayanan> service;

	public MasterServicesController() {
		super();

		service = new LayananServices();

		formValidator = new FormValidator();
	}

	public void setup() {
		updateButton.setVisible(false);
		deleteButton.setVisible(false);
		idField.setVisible(false);

		namaField.setEditable(false);
		merkField.setEditable(false);
		submerkField.setEditable(false);
	}

	public void search() {
		dialogMasterService = new MasterServiceDialog();

		try {
			if (dialogMasterService.getDetailLayanan() != null) {
				String id = dialogMasterService.getDetailLayanan().getId().toString();
				String nama = dialogMasterService.getDetailLayanan().getLayanan().getNama();
				String harga = dialogMasterService.getDetailLayanan().getHarga().toBigInteger().toString();
				String merk = dialogMasterService.getDetailLayanan().getDetail_kendaraan().getKendaraan().getMerk();
				String subMerk = dialogMasterService.getDetailLayanan().getDetail_kendaraan().getSub_merk();

				idField.setText(id);
				namaField.setText(nama);
				hargaField.setText(harga);
				merkField.setText(merk);
				submerkField.setText(subMerk);

				saveButton.setVisible(false);
				updateButton.setVisible(true);
				deleteButton.setVisible(true);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	public void get() {
		throw new UnsupportedOperationException("Not implemented");
	}

	public void reset() {
		idField.setText("");
		namaField.setText("");
		hargaField.setText("");
		merkField.setText("");
		submerkField.setText("");
	}

	public void save() {
		if (!isFormValid(getComponent())) {
			return;
		}

		try {
			service.saveOrUpdate(getComponentData());

			JOptionPane.showMessageDialog(this, "Simpan data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			reset();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Simpan data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void update() {
		if (!isFormValid(getComponent())) {
			return;
		}

		try {
			service.saveOrUpdate(getComponentData());

			JOptionPane.showMessageDialog(this, "Ubah data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			cancel();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Ubah data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void delete() {
		if (!isFormValid(getComponent())) {
			return;
		}

		try {
			service.delete(getComponentData());

			JOptionPane.showMessageDialog(this, "Hapus data sukses", "Pesan", JOptionPane.INFORMATION_MESSAGE);
			cancel();
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Hapus data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void cancel() {
		reset();

		try {
			saveButton.setVisible(true);
			updateButton.setVisible(false);
			deleteButton.setVisible(false);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	protected boolean isFormValid(JTextComponent... component) {
		try {
			formValidator.isValid(component);

			return true;
		} catch (IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this, "Data kosong!", "Pesan", JOptionPane.WARNING_MESSAGE);
		}

		return false;
	}

	protected void getServiceType() {
		dialogMasterServiceType = new MasterServiceTypeDialog();

		try {
			if (dialogMasterServiceType.getJenisLayanan() != null) {
				long id = dialogMasterServiceType.getJenisLayanan().getId();
				targetedSvcType = id;

				String nama = dialogMasterServiceType.getJenisLayanan().getNama();
				namaField.setText(nama);
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}

	protected void getVehicle() {
		dialogMasterVehicle = new MasterVehicleDialog();

		try {
			if (dialogMasterVehicle.getMerk() != null
			&&  dialogMasterVehicle.getSubMerk() != null
			&& !dialogMasterVehicle.getMerk().isEmpty()
			&& !dialogMasterVehicle.getSubMerk().isEmpty()) {
				merkField.setText(dialogMasterVehicle.getMerk());
				submerkField.setText(dialogMasterVehicle.getChosenSubMerk());
			}
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}
}